#pragma once
#pragma execution_character_set("utf-8")
#include "pch.h"
#include "ui_SurfaceReconstruction.h"
#include <QTextCodec>
#include <QFileDialog>
#include <QCoreApplication>
#include <QLineEdit>
#include <QMessageBox>
#include <QInputDialog>
#include <QApplication>
#include <QMouseEvent>
#include <QThread>
#include <QMetatype>
#include <QDir>
#include <QtWidgets/QMainWindow>
#include <vtkAutoInit.h> 
#include <vtkRenderWindow.h>
#include "FilterPipeSystem.h"
#include "PointCloud.h"
#include "Mesh.h"
#include "ExecFilterPipe.h"
#include "DXFReader.h"

namespace AutomatedSurfaceReconstruction 
{
	class CSurfaceReconstruction : public QMainWindow
	{
		Q_OBJECT

	public:
		CSurfaceReconstruction(QWidget* vParent = Q_NULLPTR);
		~CSurfaceReconstruction();

		void init();

	signals:
		void StartLoadingPointCloud(std::vector<std::string> vFilePathSet, std::unordered_map<std::string, PCLPointSetType::Ptr> vFilePath2PointCloud);
		void StartLoadingMesh(std::vector<std::string> vFilePathSet, std::unordered_map<std::string, PCLMeshType::Ptr> vFilePath2Mesh);
		void StartFilterPipe(std::unordered_map<std::string, std::pair<CGALPointSetType, CGALPointSetType>> vPointCloud2RoadData);
		
	private:
		QTextCodec* m_pCode;
		Ui::CSurfaceReconstructionClass m_UI;
		pcl::visualization::PCLVisualizer::Ptr m_pViewer;
		std::unordered_map<std::string, pcl::PolygonMesh::Ptr> m_FilePath2Mesh;
		std::unordered_map<std::string, PCLPointSetType::Ptr> m_FilePath2PointCloud;
		std::unordered_map<std::string, std::pair<CGALPointSetType, CGALPointSetType>> m_SimpfiedPointCloudAndDXFPointCloud;

		CPointCloud* m_pLoadPointCloud;	
		CMesh* m_pLoadMesh;		
		CExecFilterPipe* m_pFilterPipe;
		QThread m_LoadPointCloudThread;
		QThread m_LoadMeshThread;
		QThread m_FilterPipeThread;

		bool m_ReadLogFileFlag;
		int m_ReadLogFileSeek;

		void __connectSignals();
		void __initVTKWidget();
		void __updateVTKWidget(const PCLMeshType::Ptr& vMesh);
		void __updateVTKWidget(const PCLPointSetType::Ptr& vPointCloud);
		void __addItem2ListWidget(const std::string& vItemText, QListWidget* vopListWidget);
		void __clearFilePath2PointCloud();
		void __clearFilePath2Mesh();
		void __getDXFSet(std::vector<std::string>& vioLasFilePathSet, std::vector<std::string>& voDXFFilePathSet);
		long long __convertQString2LongLong(QString vData);
		std::string __generateLogInfo(const std::string& vLogInfo);
		void __ReadLogFile(std::string vLogFilePath);
		void __clearListItem();
		void __addHighLightPointCloud(const CGALPointSetType& vCGALPointCloud);
		
	private slots:
		void onActionOpen();
		void onActionExit();
		void onActionSurfaceReconstruct();
		void onActionClickPointCloudListItem(QListWidgetItem* item);
		void onActionClickMeshListItem(QListWidgetItem* item);
		void onActionRightClickPointCloudItem(QPoint pos);
		void onActionDeletePointCloudItem();
		void onActionClearPointCloudItem();
		void onActionRightClickMeshItem(QPoint pos);
		void onActionDeleteMeshItem();
		void onActionClearMeshItem();
		void onActionLoadOnePointCloud(std::string vFilePath, PCLPointSetType::Ptr vOriginPointCloud, CGALPointSetType vSimpfiedPointCloud, CGALPointSetType vDXFPointCloud);
		void onActionLoadAllPointCloud();
		void onActionLoadOneMesh(std::string vFilePath, PCLMeshType::Ptr vFilePath2Mesh);
		void onActionLoadAllMesh();
		void onActionReceiveResult();
		void onActionSetButtonFalse();
		void onActionSetButtonTure();
		void onActionReconstructOneMesh(std::string vMeshPath);
	};

}